// pages/memo/memo.js
const app = getApp();
var network = require("../../utils/net.js");
Page({
  data: {
    isNull: true, //随笔是否为空
    isLayOpen: false,
    Mleft: '',
    Mright: '40px',
    Mtop: '',
    Mbottom: '40px',
    isOperate: false, //是否为文件操作状态
    memoWidth: '100%', //便签默认宽度
    hasMore: false,
    loadText: '加载中...',
    memoWrapperHeight: '', //便签列表父盒子高度
    memoList: [], //便签列表
    total: 0, //便签总条数
    limit: 10, //每页多少条数据
    page: 1, //第几页
  },

  onShow: function (options){
    var _that = this;
    //如果加载的是第一页
    _that.setData({
      page: 1
    });
    //加载数据
    _that.initData();
  },

  onLoad: function(options) {
  },

  //加载数据
  initData: function() {
    var _that = this;
    network.GET({
      params: {
        limit: _that.data.limit,
        page: _that.data.page,
      },
      API_URL: "/app/stickyNotes",
      success: (res) => {
        if (res.returnCode == 0) {
          //加载数据到页面
          _that.loadDataToPage(res);
        } else {
          wx.showToast({
            mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false
            title: res.returnMessage,
            icon: 'none',
          });
        }
      }
    })
  },

  //加载数据到页面
  loadDataToPage: function(res) {
    var _that = this;
    //加载数据
    if (_that.data.page == 1) {
      //如果加载的是第一页
      _that.setData({
        memoList: res.rows,
        total: res.total,
        page: _that.data.page + 1
      });
    } else {
      _that.setData({
        memoList: that.data.memoList.concat(res.rows),
        total: res.total,
        page: _that.data.page + 1
      });
    }
    //判断是否还有更多数据
    _that.judgeMoreData();
  },

  //判断是否还有更多数据
  judgeMoreData: function() {
    var _that = this;
    //判断是否还有更多数据
    if (_that.data.memoList.length >= _that.data.total) {
      _that.setData({
        hasMore: false
      })
    } else {
      _that.setData({
        hasMore: true
      })
    }
  },

  // 拖拽开始
  startMove() {
    //拖拽控制lock
    this.lock = true;
  },

  // 拖拽中
  moving(e) {
    if (this.lock) {
      this.setData({
        Mleft: e.touches[0].clientX - 30 + 'px',
        Mright: '',
        Mtop: e.touches[0].clientY - 30 + 'px',
        Mbottom: ''
      })
    }
  },

  // 拖拽结束
  endMove() {
    this.lock = false;
    this.setData({
      Mleft: '',
      Mright: '40px',
      Mtop: '',
      Mbottom: '40px',
    });
    wx.navigateTo({
      url: '../../pages/memo/addmemo?title=新建便签&status=create',
    })
  },

  // 查看便签
  toDisplay(e) {
    //获取便签id
    let idx = e.currentTarget.dataset.idx;
    //获取下标
    let index = e.currentTarget.dataset.index;
    // 非文件操作状态 跳转到展示页面
    if (!this.data.isOperate) {
      wx.navigateTo({
        url: '../../pages/memo/addmemo?title=便签&status=display&idx=' + idx,
      });
      // 文件操作状态 
    } else {
      // 文件选中或取消
      this.data.memoList[index].isCheck = !this.data.memoList[index].isCheck;
      this.setData({
        memoList: this.data.memoList
      })
    }
  },

  // 操作便签
  toOperate() {
    // 设置便签操作状态
    this.setData({
      isOperate: true,
      memoWidth: '86%'
    })
    // 避免底部操作栏遮挡标签列表
    let h = null;
    let query0 = wx.createSelectorQuery();
    query0.select('#memoWrapper').boundingClientRect();
    query0.exec(res => {
      h = res[0].height;
    })
    let query1 = wx.createSelectorQuery();
    query1.select('#operateGroup').boundingClientRect();
    query1.exec(res => {
      this.setData({
        memoWrapperHeight: h + res[0].height + 'px'
      })
    })
  },

  // 取消操作
  cancleOperate() {
    // 避免底部操作栏遮挡标签列表
    let h = null;
    let query0 = wx.createSelectorQuery();
    query0.select('#memoWrapper').boundingClientRect();
    query0.exec(res => {
      h = res[0].height;
    })
    let query1 = wx.createSelectorQuery();
    query1.select('#operateGroup').boundingClientRect();
    query1.exec(res => {
      this.setData({
        memoWrapperHeight: h - res[0].height + 'px'
      })
    })
    // 文件全选状态置空
    this.isAll = null;
    // 取消便签操作状态 恢复样式
    this.setData({
      isOperate: false,
      memoWidth: '100%'
    });
    // 清空所用选中
    this.data.memoList.forEach(function(item, index) {
      item.isCheck = false
    });
    this.setData({
      memoList: this.data.memoList
    })
  },

  // 全选或取消
  toSelectAll() {
    if (!this.isAll) {
      this.data.memoList.forEach(item => {
        item.isCheck = true;
      })
      this.isAll = true;
      //  取消选中置顶和非置顶
    } else {
      this.data.memoList.forEach(item => {
        item.isCheck = false;
      })
      this.isAll = false;
    }
    this.setData({
      memoList: this.data.memoList
    });
    //  分组页选中/取消
  },

  // 删除选中
  toDelete() {
    var _that = this;
    //要删除的数据id串
    var ids = "";
    _that.data.memoList.forEach((item, index, arr) => {
      if (item.isCheck) {
        ids += item.id + ',';
      }
    })
    wx.showModal({
      title: '删除操作',
      content: '确定删除吗？',
      success(data) {
        if (data.confirm) {
          if (!ids) {
            wx.showToast({
              mask: true,
              title: '删除失败',
              icon: 'none',
            });
          } else {
            network.POST({
              params: {
                ids: ids,
              },
              API_URL: "/app/delStickyNotes",
              success: (res) => {
                if (res.returnCode == 0) {
                  wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function(){
                    //如果加载的是第一页
                    _that.setData({
                      page: 1
                    });
                    //加载数据
                    _that.initData();
                  }, 1000);
                } else {
                  wx.showToast({
                    mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false
                    title: res.returnMessage,
                    icon: 'none',
                  });
                }
              }
            })
          }
        }
      }
    });
  },

  // 触底事件
  onReachBottom() {
    //如果数据还没加载完
    if (this.data.memoList.length > this.data.total) {
      //显示加载进度
      this.setData({
        hasMore: true
      })
      //加载数据
      this.initData();
    }
  },

  //  下拉事件
  onPullDownRefresh: function() {
    var _that = this;
    //如果加载的是第一页
    _that.setData({
      page: 1
    });
    //加载数据
    _that.initData();
  }
})